import request from '@/utils/request'

const api_name = '/admin/core/menus'

export function getMenusTree() {
  return request({
    url: `${api_name}/tree`,
    method: 'get'
  })
}

export function getMenus(params) {
  return request({
    url: '/admin/core/menus',
    method: 'get',
    params
  })
}

export function saveLevelOne(menu) {
  return request({
    url: `${api_name}/save`,
    method: 'post',
    data: menu
  })
}

export function getMenuSuperior(ids) {
  const data = Array.isArray(ids) || ids.length === 0 ? ids : Array.of(ids)
  return request({
    url: '/admin/core/menus/superior',
    method: 'post',
    data
  })
}

export function getChild(id) {
  return request({
    url: '/admin/core/menus/child?id=' + id,
    method: 'get'
  })
}

export function buildMenus() {
  return request({
    url: '/admin/core/menus/build',
    method: 'get'
  })
}

export function add(data) {
  return request({
    url: '/admin/core/menus',
    method: 'post',
    data
  })
}

export function update(menu) {
  return request({
    url: `${api_name}/update`,
    method: 'put',
    data: menu
  })
}

export function toAssign(roleId) {
  return request({
    url: `${api_name}/toAssign/${roleId}`,
    method: 'get'
  })
}

export function doAssign(data) {
  return request({
    url: `${api_name}/doAssign`,
    method: 'post',
    data
  })
}

export function removeById(id) {
  return request({
    url: `${api_name}/` + id,
    method: 'delete'
  })
}

export function edit(data) {
  return request({
    url: `${api_name}`,
    method: 'put',
    data
  })
}

export default {
  add, edit, removeById, getMenusTree, getMenuSuperior,
  getMenus, saveLevelOne, getChild, update, toAssign, doAssign
}
